﻿using System;
using System.Collections.Generic;
using OWL.Rendering.HRP;
using UnityEngine;
using UnityEngine.Rendering;

namespace Assets.Scripts.Art.RpgScene
{
    public class PveVolumeEvent : MonoBehaviour
    {
        private float animTimer; //计时器

        [Header("当前区域名称：")] public string areaName = "areaName"; 
        public string playerColliderName = "player_model_mid_RPG_controller_Root(Clone)"; //主角碰撞体 

        public List<VolumeProfile> pveVolumes; //两个具有高度雾的后效文件

        private bool _isPlayVolumeAnimation = false; //播放状态
        private bool _isResetVolumeAnimation = false; //重置状态

        public AnimationCurve fogHeight;
        public AnimationCurve fogDensity;
        public AnimationCurve fogHeightFallOff;
        public AnimationCurve fogMaxOpacity;
        public AnimationCurve fogStartDistance;
        public Gradient fogColorGradient;

        void Start()
        {
            //把后效的默认参数存到动画曲线的第一个Key中，默认两个雾效参数是一样的,所以只拿了第一个雾效参数
            if (pveVolumes[0].TryGet(typeof(HHeightFog), out HHeightFog component))
            {
                fogDensity.keys[0].value = component.fogDensity.value;
                fogHeight.keys[0].value = component.fogHeight.value;
                fogHeightFallOff.keys[0].value = component.fogHeightFallOff.value;
                fogMaxOpacity.keys[0].value = component.fogMaxOpacity.value;
                fogStartDistance.keys[0].value = component.fogStartDistance.value;
            }
        }

        // Update is called once per frame
        void Update()
        {
            PlayVolumeAnimation();
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other.name == playerColliderName)
            {
                _isPlayVolumeAnimation = true;
                _isResetVolumeAnimation = false;
                Debug.Log("玩家已进入" + "'" + areaName + "'" + "区域！" + "并说了一句：这里景色真好看，我要带我朋友来玩！");
            }
            //Debug.Log("玩家"+ other.name +"已进入" + "'" + areaName + "'" + "区域！" );
        }

        private void OnTriggerExit(Collider other)
        {
            if (other.name == playerColliderName)
            {
                _isResetVolumeAnimation = true;
                _isPlayVolumeAnimation = false;
                Debug.Log("玩家已离开" + "'" + areaName + "'" + "区域！" + "并说了一句：我一定会回来的！");
            }
            //Debug.Log("玩家"+ other.name +"已离开" + "'" + areaName + "'" + "区域！" );
        }

        private void PlayVolumeAnimation()
        {
            if (_isPlayVolumeAnimation)
            {
                if (animTimer <= fogDensity.keys[fogDensity.length - 1].time)
                {
                    animTimer += Time.deltaTime;
                }
                else
                {
                    _isPlayVolumeAnimation = false;
                }

                for (int i = 0; i < pveVolumes.Count; i++)
                {
                    if (pveVolumes[i].TryGet(typeof(HHeightFog), out HHeightFog component))
                    {
                        component.fogDensity.value = fogDensity.Evaluate(animTimer);
                        component.fogHeight.value = fogHeight.Evaluate(animTimer);
                        component.fogHeightFallOff.value = fogHeightFallOff.Evaluate(animTimer);
                        component.fogMaxOpacity.value = fogMaxOpacity.Evaluate(animTimer);
                        component.fogStartDistance.value = fogStartDistance.Evaluate(animTimer);
                        //渐变色的Time范围在0-1，所以要求比值
                        component.fogColor.value =
                            fogColorGradient.Evaluate(animTimer / fogDensity.keys[fogDensity.length - 1].time);
                    }
                }
            }

            if (_isResetVolumeAnimation)
            {
                if (animTimer >= 0.0f)
                {
                    animTimer -= Time.deltaTime;
                }
                else
                {
                    _isResetVolumeAnimation = false;
                }

                for (int i = 0; i < pveVolumes.Count; i++)
                {
                    if (pveVolumes[0].TryGet(typeof(HHeightFog), out HHeightFog component))
                    {
                        component.fogDensity.value = fogDensity.Evaluate(animTimer);
                        component.fogHeight.value = fogHeight.Evaluate(animTimer);
                        component.fogHeightFallOff.value = fogHeightFallOff.Evaluate(animTimer);
                        component.fogMaxOpacity.value = fogMaxOpacity.Evaluate(animTimer);
                        component.fogStartDistance.value = fogStartDistance.Evaluate(animTimer);
                        //渐变色的Time范围在0-1，所以要求比值
                        component.fogColor.value =
                            fogColorGradient.Evaluate(animTimer / fogDensity.keys[fogDensity.length - 1].time);
                    }
                }
            }
        }
    }
}